# -*- coding: utf-8 -*-
# @Time    : 2021/3/11 10:26
# @Author  : zcn
# @Software: PyCharm
import asyncio
import json
import random
from contextlib import asynccontextmanager

from fastapi import APIRouter, Depends
from re_common.baselibrary.utils.basedict import BaseDicts
from re_common.baselibrary.utils.core.mlamada import bools_string

from apps.allsubdb.proxies_control.models import PostProxy
from apps.core.global_model import InputPlatformModel
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import ReturnInfo, FAILED, SUCCESS
from apps.crawler_platform.util.sqlhelper import SQLHelper
from apps.sql_app.database import proxys_sql_conn

router = APIRouter(route_class=ContextIncludedRoute)


###############################################################################################################
# 函数区
###############################################################################################################

lock = asyncio.Lock()
async def get_lock():
    await lock.acquire()
    try:
        yield lock
    finally:
        lock.release()

count = 0

@router.get("/api", dependencies=[Depends(get_lock)])
async def api_endpoint():
    # 执行需要互斥操作的代码块
    global count
    count += 1
    return count


async def sql_select():
    # 取现有活跃代理
    sql = "select proxy from proxyall_v1 where sources != 'school' and stat = 1"
    sql_as = "select DISTINCT proxy from proxy_management  where task_name not in ('school','fictitious_proxy')"
    rows = await SQLHelper.fetchall(sql)

    proxy_list = [row[0] for row in rows]
    rows_as = await SQLHelper.fetchall(sql_as)

    proxy_list_as = [row_as[0] for row_as in rows_as]
    return proxy_list, proxy_list_as


async def merge_pxoxy():
    delete_list = list()
    # noname 为一个标识，标识为普通代理
    sql_insert = "insert ignore into proxy_management(proxy, task_name, task_tag) Values(%s, 'noname', 'noname')"
    sql_delete = "delete from proxy_management where proxy=%s and task_name = 'noname'"
    # 获取 proxy_v1代理 和 proxy_management 中已经存在的代理
    proxy_row, own_proxy_row = await sql_select()
    # 删除 不存在IP以及为noname的
    if len(own_proxy_row) != 0:
        # 差集在own_proxy_row有proxy_row没有的值
        delete_list = list(set(own_proxy_row).difference(set(proxy_row)))
        if delete_list:
            # 这部分代理失效删除这部分代理
            await SQLHelper.executemany_commit(sql_delete, delete_list)
    insert_args = [(row,) for row in proxy_row]
    # 插入新的IP，这些代理新扫描出来的
    await SQLHelper.executemany_commit(sql_insert, insert_args)
    return delete_list


async def delete_data(slist):
    sql_clean = "delete from proxy_management where proxy = %s and task_name not in ('school','fictitious_proxy','abroad')"
    sql_args = [(value,) for value in slist]
    await SQLHelper.executemany_commit(sql_clean, sql_args)
    return True


async def update_counts():
    sql_update = "update proxy_management set counts = 0"
    await SQLHelper.execute_commit(sql_update)
    return True


async def sql_select_tasks():
    # 在taskinfo_save_setting获取所有任务的name+tag
    sql = "select task_name, task_tag from taskinfo_save_setting"
    rows = await SQLHelper.fetchall(sql)
    return rows


async def update_redis():
    from main import app
    redis = app.state.redis
    rows = await sql_select_tasks()
    for row in rows:
        _id = "_".join([row[0], row[1]])
        await redis.hset("proxy_counts", _id, 0)

    return True


async def update_redis_plus(key):
    from main import app
    redis = app.state.redis
    await redis.hincrby("proxy_counts", key)
    return True


async def sql_select_as():
    # 在自检表内取代理列表
    sql = "select proxy from proxy_management  where task_name = 'noname'"
    rows = await SQLHelper.fetchall(sql)
    row_list = [row[0] for row in rows]
    return row_list


async def sql_select_school():
    # 在自检表内取代理列表
    sql = "select proxy from proxy_management  where task_name in ('school','fictitious_proxy') and counts = 0"
    rows = await SQLHelper.fetchall(sql)
    row_list = [row[0] for row in rows]
    return row_list


async def sql_select_abroad():
    # 在自检表内取代理列表
    sql = "select proxy from proxy_management  where task_name in ('abroad')"
    rows = await SQLHelper.fetchall(sql)
    row_list = [row[0] for row in rows]
    return row_list


async def sql_update_noname(proxy):
    # 更新代理标识为了获得更新时间方便后期取用
    sql_as = "update proxy_management set counts = counts+1 where proxy = %s and task_name = 'noname'"
    await SQLHelper.execute_commit(sql_as, (proxy,))


async def sql_update(proxy, TKname, SPname):
    # 更新自检表 前一个更新次数方便控制, 后一个更新代理标识为了获得更新时间方便后期取用
    sql = "update proxy_management set counts = counts+1 where proxy = %s and task_name = %s and task_tag = %s"
    await SQLHelper.execute_commit(sql, (proxy, TKname, SPname))


async def proxy_times_ctrl(TKname, SPname):
    # 根据传入的task_name和task_tag进行查询  获取代理控制次数
    # 新增同时获取代理种类
    from main import app
    redis = app.state.redis
    _id = "_".join([TKname, SPname])
    if _id != "chaoxing_cookie_cx_cookie":
        taskinfo_save_setting = await redis.hget("taskinfo_save_setting", _id)
        if not taskinfo_save_setting:
            raise Exception("不能获取代理, 因为输入参数错误: ", _id)
        data = json.loads(taskinfo_save_setting)
        proxy_count = BaseDicts.is_dict_exit_key(data, "proxy_ctrl", default=3)
        proxy_type = BaseDicts.is_dict_exit_key(data, "proxy_type", default="normal")
    else:
        proxy_count = 3
        proxy_type = "school"
    return proxy_count, proxy_type


async def proxy_count_now(key):
    from main import app
    redis = app.state.redis
    counts_data = await redis.hget("proxy_counts", key)
    if not counts_data:
        raise Exception("不能获取代理的当前次数, 检查输入参数或等待刷新: ", key)
    return counts_data


###############################################################################################################
# API区
###############################################################################################################


@router.get("/proxyupdate/proxies_management")
async def get_proxy():
    """
    该接口应该没分钟调度一次
    :return:
    """
    return_info = ReturnInfo()
    delete_list = await merge_pxoxy()
    bools = await delete_data(delete_list)
    if bools:
        return_info.status = SUCCESS
        return_info.msg_code = 200
        return_info.msg = "代理获取成功"
        return_info.data = delete_list
        return return_info.todict()
    else:
        return_info.status = FAILED
        return_info.msg_code = 200
        return_info.msg = "代理更新失败"
        return_info.data = ''
        return return_info.todict()


@router.get("/proxyupdate/proxies_update")
async def get_proxy():
    from main import app
    return_info = ReturnInfo()
    # bools = await update_counts()
    bools = await update_redis()
    if not hasattr(app.state, "proxy_dict"):
        app.state.proxy_dict = dict()
    app.state.proxy_list = await sql_select_as()
    for key, value in app.state.proxy_dict.items():
        proxy_list = app.state.proxy_list.copy()
        app.state.proxy_dict[key] = proxy_list
    if not hasattr(app.state, "proxy_dict_school"):
        app.state.proxy_dict_school = dict()
    app.state.proxy_list_school = await sql_select_school()
    for key, value in app.state.proxy_dict_school.items():
        proxy_list_school = app.state.proxy_list_school.copy()
        app.state.proxy_dict_school[key] = proxy_list_school
    if not hasattr(app.state, "proxy_dict_abroad"):
        app.state.proxy_dict_abroad = dict()
    app.state.proxy_list_abroad = await sql_select_abroad()
    for key, value in app.state.proxy_dict_abroad.items():
        proxy_list_abroad = app.state.proxy_list_abroad.copy()
        app.state.proxy_dict_abroad[key] = proxy_list_abroad
    if bools:
        return_info.status = SUCCESS
        return_info.msg_code = 200
        return_info.msg = "代理更新成功"
        return_info.data = ''
        return return_info.todict()
    else:
        return_info.status = FAILED
        return_info.msg_code = 200
        return_info.msg = "代理更新失败"
        return_info.data = ''
        return return_info.todict()


@router.get("/getproxy/secret_proxy_test")
async def get_proxy(sources: str = '', is_all: int = 1, help: int = 0):
    return_info = ReturnInfo()
    from apps.allsubdb.proxies_control.proxy_class import ProxyClass
    proxy_cls = ProxyClass()
    proxy_dict = proxy_cls.proxy_dict
    if help == 1:
        sources_s = list()
        for k, v in proxy_dict.items():
            if v:
                sources_s.append(k)
        result = f'当前 可使用 sources 为 {";".join(sources_s)}'
        return_info.status = bools_string(True)
        return_info.msg_code = 200
        return_info.msg = ""
        return_info.data = result
        return return_info.todict()
    if sources:
        result = proxy_dict[sources]
    else:
        result = list()
        for k, v in proxy_dict.items():
            if k == 'socks':
                continue
            result.extend(v)
    if is_all != 1:
        result = random.choice(result) if result else ''
    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()


@router.get("/getproxy/secret_proxy")
async def journal_list():
    """
    获取私密代理
    marks: lblPageCount
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    sql = "select proxy from proxyall_v1 where sources = 'taiyang' and stat=1"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = [row[0] for row in rows]

    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()


@router.get("/getproxy/secret_proxy_mix")
async def journal_list():
    """
    获取私密代理, 不管是优质代理还是劣质代理
    marks: lblPageCount
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    sql = "select proxy from proxyall_v1 where sources = '' and stat=1"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = [row[0] for row in rows]

    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()


@router.get("/getproxy/random_one")
async def journal_list():
    """
    随机获取一个私密代理
    marks: lblPageCount
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    sql = "select proxy from proxyall_v1 where sources = 'taiyang' and stat = 1 order by rand() limit 1"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = rows[0]

    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()

@router.get("/getproxy/zdaye_random_one")
async def journal_list():
    """
    随机获取一个私密代理
    marks: lblPageCount
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    sql = "select proxy from proxy_list where sources = 'zdaye' and stat = 1 order by rand() limit 1"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = rows[0]
    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()

@router.get("/getproxy/school_random_one")
async def journal_list():
    """
    随机获取一个超星可用的学校代理
    marks: lblPageCount
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    sql = "select proxy from proxy_management where task_name = 'school' and counts = 0 order by rand() limit 1"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = rows[0]
    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()


# @router.post("/proxies_get/get_one")
# async def get_proxy(input: InputPlatformModel[PostProxy]):
#     sql_check = "select counts from proxy_management where proxy = %s and task_name = %s and task_tag = %s"
#     sql_insert = "insert ignore into proxy_management(proxy, task_name, task_tag) Values(%s, %s, %s)"
#     from main import app
#     return_info = ReturnInfo()
#     task_name = input.data.task_name
#     task_tag = input.data.task_tag
#     # 代理获取次数
#     max_proxy_get, proxy_type = await proxy_times_ctrl(task_name, task_tag)
#     key = "_".join([task_name, task_tag])
#     # 学校代理
#     if proxy_type == "school":
#         if not hasattr(app.state, "proxy_dict_school"):
#             app.state.proxy_dict_school = dict()
#         if hasattr(app.state, "proxy_list_school"):
#             proxy_list_school = BaseDicts.is_dict_exit_key(app.state.proxy_dict_school, key, default=[])
#             if not proxy_list_school:
#                 proxy_list_school_whole = app.state.proxy_list_school.copy()
#                 app.state.proxy_dict_school[key] = proxy_list_school_whole
#                 proxy_list_school = BaseDicts.is_dict_exit_key(app.state.proxy_dict_school, key, default=[])
#             try:
#                 while True:
#                     proxy = proxy_list_school.pop()
#                     row_check = await SQLHelper.fetchone(sql_check, (proxy, task_name, task_tag))
#                     if row_check is None:
#                         await SQLHelper.execute_commit(sql_insert, (proxy, task_name, task_tag))
#                         if "192.168.31.176" in proxy:
#                             return_info.status = SUCCESS
#                             return_info.msg_code = 200
#                             return_info.msg = "代理获取成功"
#                             return_info.data = proxy
#                             return return_info.todict()
#                     elif row_check[0] < max_proxy_get:
#                         await sql_update(proxy, task_name, task_tag)
#                         if "192.168.31.176" in proxy:
#                             return_info.status = SUCCESS
#                             return_info.msg_code = 200
#                             return_info.msg = "代理获取成功"
#                             return_info.data = proxy
#                             return return_info.todict()
#                     else:
#                         return_info.status = FAILED
#                         return_info.msg_code = 230
#                         return_info.msg = "代理次数在当前时间段内已取光,请稍候重试"
#                         return_info.data = ""
#                         return return_info.todict()
#                     return_info.status = FAILED
#                     return_info.msg_code = 230
#                     return_info.msg = "该代理为虚拟代理,不予返回"
#                     return_info.data = ""
#                     return return_info.todict()
#             except IndexError:
#                 if not hasattr(app.state, "proxy_judge_school") or app.state.proxy_judge_school is not True:
#                     app.state.proxy_judge_school = True
#                     try:
#                         app.state.proxy_dict_school[key].extend(app.state.proxy_list_school)
#                     except:
#                         app.state.proxy_judge_school = False
#                     finally:
#                         app.state.proxy_judge_school = False
#                         return_info.status = FAILED
#                         return_info.msg_code = 231
#                         return_info.msg = "正在处理全局列表中,请稍候重试"
#                         return_info.data = ""
#                         return return_info.todict()
#                 else:
#                     return_info.status = FAILED
#                     return_info.msg_code = 232
#                     return_info.msg = "正在处理全局列表中,请稍候重试"
#                     return_info.data = ""
#                     return return_info.todict()
#         else:
#             app.state.proxy_list_school = await sql_select_school()
#             return_info.status = FAILED
#             return_info.msg_code = 233
#             return_info.msg = "正在处理全局列表中,请稍候重试"
#             return_info.data = ""
#             return return_info.todict()
#     elif proxy_type == "abroad":
#         if not hasattr(app.state, "proxy_dict_abroad"):
#             app.state.proxy_dict_abroad = dict()
#         if hasattr(app.state, "proxy_list_abroad"):
#             proxy_list_abroad = BaseDicts.is_dict_exit_key(app.state.proxy_dict_abroad, key, default=[])
#             if not proxy_list_abroad:
#                 proxy_list_abroad_whole = app.state.proxy_list_abroad.copy()
#                 app.state.proxy_dict_abroad[key] = proxy_list_abroad_whole
#                 proxy_list_abroad = BaseDicts.is_dict_exit_key(app.state.proxy_dict_abroad, key, default=[])
#             try:
#                 while True:
#                     proxy = proxy_list_abroad.pop()
#                     row_check = await SQLHelper.fetchone(sql_check, (proxy, task_name, task_tag))
#                     if row_check is None:
#                         await SQLHelper.execute_commit(sql_insert, (proxy, task_name, task_tag))
#                         return_info.status = SUCCESS
#                         return_info.msg_code = 200
#                         return_info.msg = "代理获取成功"
#                         return_info.data = proxy
#                         return return_info.todict()
#                     elif row_check[0] < max_proxy_get:
#                         await sql_update(proxy, task_name, task_tag)
#                         return_info.status = SUCCESS
#                         return_info.msg_code = 200
#                         return_info.msg = "代理获取成功"
#                         return_info.data = proxy
#                         return return_info.todict()
#                     else:
#                         return_info.status = FAILED
#                         return_info.msg_code = 230
#                         return_info.msg = "代理次数在当前时间段内已取光,请稍候重试"
#                         return_info.data = ""
#                         return return_info.todict()
#
#             except IndexError:
#                 if not hasattr(app.state, "proxy_judge_abroad") or app.state.proxy_judge_abroad is not True:
#                     app.state.proxy_judge_abroad = True
#                     try:
#                         app.state.proxy_dict_abroad[key].extend(app.state.proxy_list_abroad)
#                     except:
#                         app.state.proxy_judge_abroad = False
#                     finally:
#                         app.state.proxy_judge_abroad = False
#                         return_info.status = FAILED
#                         return_info.msg_code = 231
#                         return_info.msg = "正在处理全局列表中,请稍候重试"
#                         return_info.data = ""
#                         return return_info.todict()
#                 else:
#                     return_info.status = FAILED
#                     return_info.msg_code = 232
#                     return_info.msg = "正在处理全局列表中,请稍候重试"
#                     return_info.data = ""
#                     return return_info.todict()
#         else:
#             app.state.proxy_list_abroad = await sql_select_abroad()
#             return_info.status = FAILED
#             return_info.msg_code = 233
#             return_info.msg = "正在处理全局列表中,请稍候重试"
#             return_info.data = ""
#             return return_info.todict()
#
#
#
#
#     else:
#         if not hasattr(app.state, "proxy_dict"):
#             app.state.proxy_dict = dict()
#         # proxy_list维护所有代理
#         if hasattr(app.state, "proxy_list"):
#             proxy_list = BaseDicts.is_dict_exit_key(app.state.proxy_dict, key, default=[])
#             if not proxy_list:
#                 proxy_list_whole = app.state.proxy_list.copy()
#                 app.state.proxy_dict[key] = proxy_list_whole
#                 proxy_list = BaseDicts.is_dict_exit_key(app.state.proxy_dict, key, default=[])
#             try:
#                 while True:
#                     proxy = proxy_list.pop()
#                     print(proxy_list)
#                     row_check = await SQLHelper.fetchone(sql_check, (proxy, task_name, task_tag))
#                     if row_check is None:
#                         await SQLHelper.execute_commit(sql_insert, (proxy, task_name, task_tag))
#                         return_info.status = SUCCESS
#                         return_info.msg_code = 200
#                         return_info.msg = "代理获取成功"
#                         return_info.data = proxy
#                         return return_info.todict()
#                     elif row_check[0] < max_proxy_get:
#                         await sql_update(proxy, task_name, task_tag)
#                         return_info.status = SUCCESS
#                         return_info.msg_code = 200
#                         return_info.msg = "代理获取成功"
#                         return_info.data = proxy
#                         return return_info.todict()
#                     else:
#                         return_info.status = FAILED
#                         return_info.msg_code = 230
#                         return_info.msg = "代理次数在当前时间段内已取光,请稍候重试"
#                         return_info.data = ""
#                         return return_info.todict()
#
#             except IndexError:
#                 if not hasattr(app.state, "proxy_judge") or app.state.proxy_judge is not True:
#                     app.state.proxy_judge = True
#                     try:
#                         app.state.proxy_dict[key].extend(app.state.proxy_list)
#                         print("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
#                     except:
#                         app.state.proxy_judge = False
#                     finally:
#                         app.state.proxy_judge = False
#                         return_info.status = FAILED
#                         return_info.msg_code = 231
#                         return_info.msg = "正在处理全局列表中,请稍候重试"
#                         return_info.data = ""
#                         return return_info.todict()
#                 else:
#                     return_info.status = FAILED
#                     return_info.msg_code = 232
#                     return_info.msg = "正在处理全局列表中,请稍候重试"
#                     return_info.data = ""
#                     return return_info.todict()
#         else:
#             app.state.proxy_list = await sql_select_as()
#             return_info.status = FAILED
#             return_info.msg_code = 233
#             return_info.msg = "正在处理全局列表中,请稍候重试"
#             return_info.data = ""
#             return return_info.todict()


@router.post("/proxies_get/get_one")
async def get_proxy(input: InputPlatformModel[PostProxy]):
    from main import app
    return_info = ReturnInfo()
    task_name = input.data.task_name
    task_tag = input.data.task_tag
    # 代理获取次数
    max_proxy_get, proxy_type = await proxy_times_ctrl(task_name, task_tag)
    key = "_".join([task_name, task_tag])
    now_proxy_get = await proxy_count_now(key)
    if int(now_proxy_get) >= max_proxy_get:
        return_info.status = FAILED
        return_info.msg_code = 230
        return_info.msg = "代理次数在当前时间段内已取光,请稍候重试"
        return_info.data = key
        return return_info.todict()
    # 学校代理
    if proxy_type == "school":
        if not hasattr(app.state, "proxy_dict_school"):
            app.state.proxy_dict_school = dict()
        if hasattr(app.state, "proxy_list_school"):
            proxy_list_school = BaseDicts.is_dict_exit_key(app.state.proxy_dict_school, key, default=[])
            if not proxy_list_school:
                proxy_list_school_whole = app.state.proxy_list_school.copy()
                app.state.proxy_dict_school[key] = proxy_list_school_whole
                proxy_list_school = BaseDicts.is_dict_exit_key(app.state.proxy_dict_school, key, default=[])
                await update_redis_plus(key)
            # print(proxy_list_school)
            # try:
            # while True:
            proxy = random.choice(proxy_list_school)
            if "192.168.31.176" in proxy:
                return_info.status = SUCCESS
                return_info.msg_code = 200
                return_info.msg = "代理获取成功"
                return_info.data = proxy
                return return_info.todict()
            else:
                return_info.status = FAILED
                return_info.msg_code = 230
                return_info.msg = "该代理为虚拟代理,不予返回"
                return_info.data = ""
                return return_info.todict()
            # except IndexError:
            #     if not hasattr(app.state, "proxy_judge_school") or app.state.proxy_judge_school is not True:
            #         app.state.proxy_judge_school = True
            #         try:
            #             app.state.proxy_dict_school[key].extend(app.state.proxy_list_school)
            #         except:
            #             app.state.proxy_judge_school = False
            #         finally:
            #             app.state.proxy_judge_school = False
            #             return_info.status = FAILED
            #             return_info.msg_code = 231
            #             return_info.msg = "正在处理全局列表中,请稍候重试"
            #             return_info.data = ""
            #             return return_info.todict()
            #     else:
            #         return_info.status = FAILED
            #         return_info.msg_code = 232
            #         return_info.msg = "正在处理全局列表中,请稍候重试"
            #         return_info.data = ""
            #         return return_info.todict()
        else:
            app.state.proxy_list_school = await sql_select_school()
            return_info.status = FAILED
            return_info.msg_code = 233
            return_info.msg = "正在处理全局列表中,请稍候重试"
            return_info.data = ""
            return return_info.todict()
    elif proxy_type == "abroad":  # 国外代理
        if not hasattr(app.state, "proxy_dict_abroad"):
            app.state.proxy_dict_abroad = dict()
        if hasattr(app.state, "proxy_list_abroad"):
            proxy_list_abroad = BaseDicts.is_dict_exit_key(app.state.proxy_dict_abroad, key, default=[])
            if not proxy_list_abroad:
                proxy_list_abroad_whole = app.state.proxy_list_abroad.copy()
                app.state.proxy_dict_abroad[key] = proxy_list_abroad_whole
                proxy_list_abroad = BaseDicts.is_dict_exit_key(app.state.proxy_dict_abroad, key, default=[])
                await update_redis_plus(key)
            try:
                while True:
                    proxy = proxy_list_abroad.pop()
                    return_info.status = SUCCESS
                    return_info.msg_code = 200
                    return_info.msg = "代理获取成功"
                    return_info.data = proxy
                    return return_info.todict()

            except IndexError:
                if not hasattr(app.state, "proxy_judge_abroad") or app.state.proxy_judge_abroad is not True:
                    app.state.proxy_judge_abroad = True
                    try:
                        app.state.proxy_dict_abroad[key].extend(app.state.proxy_list_abroad)
                    except:
                        app.state.proxy_judge_abroad = False
                    finally:
                        app.state.proxy_judge_abroad = False
                        return_info.status = FAILED
                        return_info.msg_code = 231
                        return_info.msg = "正在处理全局列表中,请稍候重试"
                        return_info.data = ""
                        return return_info.todict()
                else:
                    return_info.status = FAILED
                    return_info.msg_code = 232
                    return_info.msg = "正在处理全局列表中,请稍候重试"
                    return_info.data = ""
                    return return_info.todict()
        else:
            app.state.proxy_list_abroad = await sql_select_abroad()
            return_info.status = FAILED
            return_info.msg_code = 233
            return_info.msg = "正在处理全局列表中,请稍候重试"
            return_info.data = ""
            return return_info.todict()

    else:
        if not hasattr(app.state, "proxy_dict"):
            app.state.proxy_dict = dict()
        # proxy_list维护所有代理
        if hasattr(app.state, "proxy_list"):
            proxy_list = BaseDicts.is_dict_exit_key(app.state.proxy_dict, key, default=[])
            if not proxy_list:
                proxy_list_whole = app.state.proxy_list.copy()
                app.state.proxy_dict[key] = proxy_list_whole
                proxy_list = BaseDicts.is_dict_exit_key(app.state.proxy_dict, key, default=[])
                await update_redis_plus(key)
            try:
                while True:
                    proxy = proxy_list.pop()
                    return_info.status = SUCCESS
                    return_info.msg_code = 200
                    return_info.msg = "代理获取成功"
                    return_info.data = proxy
                    return return_info.todict()


            except IndexError:
                if not hasattr(app.state, "proxy_judge") or app.state.proxy_judge is not True:
                    app.state.proxy_judge = True
                    try:
                        app.state.proxy_dict[key].extend(app.state.proxy_list)
                    except:
                        app.state.proxy_judge = False
                    finally:
                        app.state.proxy_judge = False
                        return_info.status = FAILED
                        return_info.msg_code = 231
                        return_info.msg = "正在处理全局列表中,请稍候重试"
                        return_info.data = ""
                        return return_info.todict()
                else:
                    return_info.status = FAILED
                    return_info.msg_code = 232
                    return_info.msg = "正在处理全局列表中,请稍候重试"
                    return_info.data = ""
                    return return_info.todict()
        else:
            app.state.proxy_list = await sql_select_as()
            return_info.status = FAILED
            return_info.msg_code = 233
            return_info.msg = "正在处理全局列表中,请稍候重试"
            return_info.data = ""
            return return_info.todict()


@router.post("/proxies_get/get_one_redis")
async def get_proxy_redis(input: InputPlatformModel[PostProxy]):
    # 思路为使用redis的队列控制代理，目前有其他任务，暂停开发
    from main import app
    return_info = ReturnInfo()
    task_name = input.data.task_name
    task_tag = input.data.task_tag
    # 代理获取次数
    max_proxy_get, proxy_type = await proxy_times_ctrl(task_name, task_tag)
    key = "_".join([task_name, task_tag])
    now_proxy_get = await proxy_count_now(key)
    if int(now_proxy_get) >= max_proxy_get:
        return_info.status = FAILED
        return_info.msg_code = 230
        return_info.msg = "代理次数在当前时间段内已取光,请稍候重试"
        return_info.data = key
        return return_info.todict()
    if proxy_type == "normal":
        pass


@router.get("/proxies_get/redis_random_one")
async def redis_proxy():
    """
    随机获取一个私密代理
    marks: lblPageCount
    :param input:
    :return:
    """
    from main import app
    redis = app.state.redis
    counts_data = await redis.srandmember("proxy_test")
    result = {"proxy": counts_data}
    return_info = ReturnInfo()
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()


@router.get("/proxies_get/qingting_redis_random_one")
async def redis_proxy():
    """
    随机获取一个蜻蜓代理
    marks: lblPageCount
    :param input:
    :return:
    """
    # from main import app
    # redis = app.state.redis
    #
    # counts_data = await redis.srandmember("proxy_qingting")
    from apps.allsubdb.proxies_control.proxy_class import ProxyClass
    proxy_cls = ProxyClass()
    proxy_dict = proxy_cls.proxy_dict
    result = proxy_dict['bees']
    # result = list()
    # for k, v in proxy_dict.items():
    #     if k == 'socks':
    #         continue
    #     result.extend(v)
    result = {"proxy": random.choice(result)}
    return_info = ReturnInfo()
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "a"
    return_info.data = result
    return return_info.todict()


@router.get("/getproxy/qingting_secret_proxy")
async def journal_list():
    """
    获取蜻蜓私密代理
    marks: lblPageCount
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    sql = "select proxy from proxyall_v2 where sources = 'qingting' and stat=1"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = [row[0] for row in rows]

    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    return_info.data = result
    return return_info.todict()

@router.post("/getproxy/new_secret_proxy")
async def journal_list(input: PostProxy):
    """
    获取站大爷代理
    marks: lblPageCount
    :param input:
    :return:
    """
    task_name = input.task_name
    return_info = ReturnInfo()
    sql = f"select proxy from proxy_list where sources = '{task_name}' and stat=1 order by rand() limit 1"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = [row[0] for row in rows]

    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    # return_info.data = result
    return_info.data = {"proxy": result[0]}
    return return_info.todict()


@router.get("/getproxy/zdaye_secret_proxy")
async def journal_list():
    """
    获取站大爷代理
    marks: lblPageCount
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    sql = "select proxy from proxy_list where stat=1 limit 100"
    rows = await proxys_sql_conn.fetch_all(query=sql)
    result = [row[0] for row in rows]
    return_info.status = bools_string(True)
    return_info.msg_code = 200
    return_info.msg = ""
    # return_info.data = result
    return_info.data = {"proxy": random.choice(result)}
    return return_info.todict()